package model

import (
	"stock/global"
)

type ColumnModel struct {
	Base
}

type SysColumn struct {
	Id         uint   `json:"id" gorm:"<-:false; primaryKey;column:id;type:int(11)"`
	Name       string `json:"name" gorm:"column:name;"`
	ColumnType string `json:"column_type" gorm:"column:column_type;"`
	Module     string `json:"module" gorm:"column:module;"`
	Cover      string `json:"cover" gorm:"column:cover;"`
	Lang       string `json:"lang" gorm:"<-:false; column:lang;"`
	CreateAt   string `json:"create_at" gorm:"<-:false; column:create_at;"`
	UpdatedAt  string `json:"updated_at" gorm:"column:updated_at;"`
}

func (m *ColumnModel) TabName() string {
	return global.CONF.Mysql.Prefix + "sys_column"
}

func (m *ColumnModel) List(id uint, whereParam map[string]string, limit int) ([]SysColumn, error) {

	var column []SysColumn
	where := &SysColumn{}
	if whereParam["column_type"] != "" {
		where.ColumnType = whereParam["column_type"]
	}
	if whereParam["column_type"] != "" {
		where.Module = whereParam["module"]
	}
	if whereParam["lang"] != "" {
		where.Lang = whereParam["lang"]
	}
	if limit == 0 {
		limit = 10000
	}
	err := global.DB.Debug().Where(where).Order("id DESC").Limit(limit).Find(&column)
	if err.Error != nil {
		return nil, err.Error
	}
	return column, nil
}
